//
//  HomeViewController.m
//  AffectionBee
//
//  Created by 李通 on 16/8/18.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "HomeViewController.h"
#import "ScannerViewController.h"
#import "SearchViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "IconsAndActivitiesModel.h"
#import "FirstCellModel.h"


@interface HomeViewController ()


//声明一个4按钮数据数组
@property (nonatomic,strong) NSArray <IconsAndActivitiesModel *> *iconsArray;

//声明一个新年惠tableView数据数组
@property (nonatomic,strong) NSArray <IconsAndActivitiesModel *> *activitiesArray;

//声明一个新鲜热卖数据数组
@property (nonatomic,strong) NSArray <FirstCellModel *> *firstCellArray;

//图片轮播器的数据数组
@property (nonatomic,strong) NSArray <IconsAndActivitiesModel *> *adArray;

@property (nonatomic,assign) NSInteger index;


@end

@implementation HomeViewController


//懒加载按钮数据
-(NSArray <IconsAndActivitiesModel *> *)iconsArray
{
    if (_iconsArray == nil)
    {
        [IconsAndActivitiesModel GetIconDataArrayWithDataType:@"icons" Sucess:^(NSArray<IconsAndActivitiesModel *> *dataArray) {
            
            _iconsArray = dataArray;
            
            
            NSLog(@"%@",dataArray[0].name);
            NSLog(@"%@",dataArray[1].name);
            NSLog(@"%@",dataArray[2].name);
            
            
        } Error:^{
            
            NSLog(@"网络数据获取失败");
            
        }];
        
    }
    
    return _iconsArray;
}

//懒加载tableView数据数组
-(NSArray <IconsAndActivitiesModel *> *)activitiesArray
{
    if (_activitiesArray == nil)
    {
        [IconsAndActivitiesModel GetIconDataArrayWithDataType:@"activities" Sucess:^(NSArray<IconsAndActivitiesModel *> *dataArray) {
            
            _activitiesArray = dataArray;
            
            
            NSLog(@"%@",dataArray[0].name);
            NSLog(@"%@",dataArray[1].name);
            NSLog(@"%@",dataArray[2].name);
            
            
        } Error:^{
            
            NSLog(@"网络数据获取失败");
            
        }];
        
    }
    
    return _activitiesArray;
}
//懒加载新鲜热卖数组
-(NSArray <FirstCellModel *> *)firstCellArray
{
    if (_firstCellArray == nil)
    {
        //(1)urlString
        NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/firstSell.json.php";
        
        //(2)参数
        NSDictionary *param = @{@"call":@2};
        
        [FirstCellModel GetFirstCellDataArrayWithUrl:urlString andParameter:param Sucess:^(NSArray<FirstCellModel *> *dataArray) {
            
            _firstCellArray = dataArray;
            
            NSLog(@"%@",dataArray[0].name);
            NSLog(@"%@",dataArray[1].name);
            NSLog(@"%@",dataArray[2].name);
            
            NSLog(@"%@",dataArray[0].brand_name);
            NSLog(@"%@",dataArray[1].brand_name);
            NSLog(@"%@",dataArray[2].brand_name);
            
            
        } Error:^{
            
            NSLog(@"网络数据获取失败");
            
        }];
        
    }
    
    return _firstCellArray;
}
//懒加载图片轮播器的数据数组
-(NSArray <IconsAndActivitiesModel *> *)adArray
{
    if (_adArray == nil)
    {
        [IconsAndActivitiesModel GetIconDataArrayWithDataType:@"focus" Sucess:^(NSArray<IconsAndActivitiesModel *> *dataArray) {
            
            _adArray = dataArray;
            
            
            NSLog(@"%@",dataArray[0].toURL);
            NSLog(@"%@",dataArray[1].toURL);
            NSLog(@"%@",dataArray[2].toURL);
            
            
        } Error:^{
            
            NSLog(@"网络数据获取失败");
            
        }];
        
    }
    
    return _adArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton*button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    button.backgroundColor = [UIColor blueColor];
    [self.view addSubview:button];
    button.center = self.view.center;
    
    [button addTarget:self action:@selector(didClickBadge) forControlEvents:UIControlEventTouchUpInside];
    
    [self setupCodeScanner];
    
    //NSLog(@"%@",self.adArray);
    //NSLog(@"%@",self.firstCellArray);
}

/*  首页左上角的扫码功能实现  */
- (void)setupCodeScanner {
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]init]barButtonWithTitle:@"扫一扫" titleColor:[UIColor blackColor] image:[[UIImage imageNamed:@"icon_black_scancode"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] hightLightImage:nil target:self action:@selector(didClickScanner) itemLoaction:YES];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]init]barButtonWithTitle:@"搜索" titleColor:[UIColor blackColor] image:[[UIImage imageNamed:@"icon_search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] hightLightImage:nil target:self action:@selector(didClickSearch) itemLoaction:NO];
}
//跳转到扫码界面
- (void)didClickScanner {
    
    [self.navigationController pushViewController:[[ScannerViewController alloc] init] animated:YES];
}
//跳转到搜索界面
- (void)didClickSearch {
    
    [self.navigationController pushViewController:[[SearchViewController alloc] init] animated:YES];
}

//MYMARK: - 实际需求中将调用控制器的blcok 给blcok进行传值即可 (可在此处修改)
- (void)didClickBadge {
    if (self.badgeValueBlock) {
        _badgeValueBlock([NSString stringWithFormat:@"%ld",self.index++]);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
